﻿function FileSize(item) {
    return jQuery.usi.tools.getFileSize(item.fileSize);
}
function AliasDown(item, eventParam) {
    return "<a style='color:#0000EE;' href='" + item.fileDownPath + "'>" + item.alias + "</a>";
}
function ReFileName(item, eventParam) {
    jQuery.usi.core.layer_show({
        type: 1,
        height: "180px",
        width: "480px",
        title: "修改文件名", content: "/Views/Annex/ReFileName.html", success: function () {
            var layerIndex = layer.index;
            $("#Alias").val(item.alias);
            $("#btnUpdate").bind("click", function () {
                if (jQuery.usi.core.isNullOrEmpty($("#Alias").val())) {
                    layer.alert("文件名不允许为空");
                } else {
                    jQuery.usi.core.post("/Annex/UpdateFileName", { AnnexUnid: item.unid, Alias: $("#Alias").val() }, false, function () {
                        jQuery.usi.core.layer_close({ index: layerIndex });
                        jQuery.usi.tools.bindDataTable.refreshData("#" + eventParam.tableId);
                    });
                }
            });
        }
    });
}
function ViewFile(item, eventParam) {
    if (item.fullPath.indexOf(".swf") >= 0) {
        jQuery.usi.core.layer_show({
            type: 1,
            title: "查看文件", width: "850px", height: "600px", content: "/Views/Annex/ViewFile.html", success: function () {
                var _filename = item.fullPath;
                var fp = new FlexPaperViewer(
                    '/lib/FlexPaper/FlexPaperViewer',
                    'viewerPlaceHolder',
                    {
                        config: {
                            SwfFile: escape(_filename),
                            Scale: 1.0,
                            ZoomTransition: 'easeOut', //其他可选值包括: easenone, easeout, linear, easeoutquad
                            ZoomTime: 0.5,
                            ZoomInterval: 0.2,
                            FitPageOnLoad: false,
                            FitWidthOnLoad: false,
                            PrintEnabled: true,
                            FullScreenAsMaxWindow: false,
                            ProgressiveLoading: false,
                            MinZoomSize: 0.2,
                            MaxZoomSize: 10,
                            SearchMatchAll: false,
                            InitViewMode: 'Portrait',
                            ViewModeToolsVisible: true,
                            ZoomToolsVisible: true,
                            NavToolsVisible: true,
                            CursorToolsVisible: true,
                            SearchToolsVisible: true,
                            localeChain: 'zh_CN'
                        }
                    });
            }
        });
    } else if (item.fullPath.indexOf(".jpeg") >= 0 || item.fullPath.indexOf(".gif") >= 0 || item.fullPath.indexOf(".png") >= 0 || item.fullPath.indexOf(".jpg") >= 0 || item.fullPath.indexOf(".bmp") >= 0) {
        jQuery.usi.core.layer_show({
            type: 1,
            title: "查看" + item.alias + "图片附件",
            height: "600px",
            width: "850px",
            content: "/Views/Annex/ViewImageFile.html",
            success: function () {
                $("#myimg").attr("src", item.fullPath);
            }
        });
    } else {
        window.open(item.fullPath);
    }
}
function DownFile(item, eventParam) {

    window.open("/Annex/AnnexDownFile?fileDownPath=" + item.fileDownPath + "&fileName=" + item.alias + (jQuery.usi.core.isNullOrEmpty(item.fileSuffix) ? "" : item.fileSuffix));
}
function AnnexDeleteFileById(item, eventParam) {
    jQuery.usi.core.deletePost("/Annex/DeleteFile", { Ids: item.unid }, function (data) {
        FileDataLoad(eventParam);
    });
}
function UploadFile(config) {
    var file = jQuery.usi.tools.uploadFile(config.controlId, {
        'uploader': '/Annex/UploadFile', //后台处理程序的相对路劲 
        'queueID': config.queueID, //上传队列的dom容器id
        'auto': true,  //true 选择完文件自动上传
        'multi': true,
        'formData': { AliasDataSort: config.aliasDataSort, AliasData: (jQuery.usi.core.isNullOrEmpty(config.aliasData) ? "" : $("#" + config.aliasData).val()), MainDataUnid: $("#" + config.dataId).val(), UserUnid: jQuery.usi.core.isNullOrEmpty(jQuery.usi.core.loginUser) ? (jQuery.usi.core.isNullOrEmpty(parent.jQuery.usi.core.loginUser) ? "" : parent.jQuery.usi.core.loginUser.user.unid) : jQuery.usi.core.loginUser.user.unid, ActivitieUnid: $("#" + config.activitieUnid).val() },
        onUploadSuccess: function (file, data, response) {
            FileDataLoad(config);
        }
    });
}
function DownQrCode(item, eventParam) {
    jQuery.usi.core.layer_show({
        type: 1,
        width: "500px",
        height: "550px",
        title: "二维码下载",
        content: "/Views/QrCodeView/ViewQrCode.html",
        success: function () {
            jQuery.usi.core.post("/Annex/CreateQrCode", { Unid: item.unid }, false, function (data) {
                var div = $("#myQrCode");
                var img = $("<img style='width:" + data.data.width + "px;height:" + data.data.height + "px' src='data:image/jpeg;base64," + data.data.data + "'/>");
                div.append(img);
            });
        }
    });
}
function FileDataLoad(config) {
    var newconfig = {
        isView: jQuery.usi.core.isNullOrEmpty(config.isView) && config.IsView ? true : config.isView,
        tableId: jQuery.usi.core.isNullOrEmpty(config.tableId) ? "AnnexTable" : config.tableId,
        dataId: jQuery.usi.core.isNullOrEmpty(config.dataId) ? "Unid" : config.dataId,
        controlId: jQuery.usi.core.isNullOrEmpty(config.controlId) ? "uploadify" : config.controlId,
        queueID: jQuery.usi.core.isNullOrEmpty(config.queueID) ? "fileQueue" : config.queueID,
        divId: jQuery.usi.core.isNullOrEmpty(config.divId) ? "AnnexDivId" : config.divId,
        aliasData: jQuery.usi.core.isNullOrEmpty(config.aliasData) ? "" : config.aliasData,
        aliasDataSort: jQuery.usi.core.isNullOrEmpty(config.aliasDataSort) ? "0" : config.aliasDataSort,
        activitieUnid: jQuery.usi.core.isNullOrEmpty(config.activitieUnid) ? "activitieUnid" : config.activitieUnid
    };
    var CusEvent;
    if (!newconfig.isView) {
        CusEvent = [
            {
                name: "重命名",
                formula: '(item.activitieUnid==$("#' + config.activitieUnid + '").val())',
                click: function (item, eventParam) {
                    ReFileName(item, eventParam);
                }
            }, {
                name: "删除",
                formula: '(item.activitieUnid==$("#' + config.activitieUnid + '").val())',
                click: function (item, eventParam) {
                    AnnexDeleteFileById(item, eventParam);
                }
            }, {
                name: "查看",
                formula: '(item.fullPath.indexOf(".swf") >= 0||item.fullPath.indexOf(".jpeg") >= 0 || item.fullPath.indexOf(".gif") >= 0 || item.fullPath.indexOf(".png") >= 0 || item.fullPath.indexOf(".jpg") >= 0 || item.fullPath.indexOf(".bmp") >= 0)',
                click: function (item, eventParam) {
                    ViewFile(item, eventParam);
                }
            }, {
                name: "下载",
                click: function (item, eventParam) {
                    DownFile(item, eventParam);
                }
            }, {
                name: "扫码下载",
                click: function (item, eventParam) {
                    DownQrCode(item, eventParam);
                }
            }
        ];
    } else {
        $("#" + newconfig.divId).hide();
        CusEvent = [
            {
                name: "查看",
                formula: '(item.fullPath.indexOf(".swf") >= 0||item.fullPath.indexOf(".jpeg") >= 0 || item.fullPath.indexOf(".gif") >= 0 || item.fullPath.indexOf(".png") >= 0 || item.fullPath.indexOf(".jpg") >= 0 || item.fullPath.indexOf(".bmp") >= 0)',
                click: function (item, eventParam) {
                    ViewFile(item, eventParam);
                }
            }, {
                name: "下载",
                click: function (item, eventParam) {
                    DownFile(item, eventParam);
                }
            }, {
                name: "扫码下载",
                click: function (item, eventParam) {
                    DownQrCode(item, eventParam);
                }
            }
        ];
    }
    var gridConfig = {
        url: "/Annex/GetFilePageData",
        param: function () { return { MainDataUnid: $("#" + newconfig.dataId).val() } },
        id: newconfig.tableId,
        isLoadOperate: false,
        customOperate: CusEvent,
        isShowPage: false,
        operateParam: newconfig
    };
    jQuery.usi.tools.bindDataTable.loadData(gridConfig);
    UploadFile(newconfig);
}
function UploadSingleFile(config) {
    var file = jQuery.usi.tools.uploadFile(config.controlId, {
        'uploader': '/Annex/UploadSingleFile', //后台处理程序的相对路劲 
        'queueID': config.queueID, //上传队列的dom容器id
        'auto': true,  //true 选择完文件自动上传
        'multi': true,
        'buttonImage': null,
        'fileTypeExts': (jQuery.usi.core.isNullOrEmpty(config.fileTypeExts) ? "*.*" : config.fileTypeExts),
        'buttonText': config.text,
        'formData': { type: "" },
        'onInit': function () {                        //载入时触发，将flash设置到最小
            $("#" + config.queueID).hide();
        },
        onUploadSuccess: function (file, data, response) {
            //ExecutionFunction(config.callback, data);
            config.callback(data);
        }
    });
}
